// pages/record/list/list.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list: [],
		time: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options)
		wx.request({
			url: getApp().globalData.http + 'action=getByList',
			success: (e) => {
				this.setData({
					id: options.id,
					data: e.data.data,
					name:options.name
				})
				wx.request({
					url: getApp().globalData.http + 'action=getBaoYangDetail&id=' + options.id,
					success: (res) => {
						console.log(res)
						let a = e.data.data
						let b=res.data.data.Contents.split(',')
						let c=this.data.list
						let that=this
						a.forEach(function(item, index) {
							b.forEach(function(li,i){
								c.push(li)
								if (li == item.names) {
									a[index].state = 1
								}
							})
							
						})
						c=[...new Set(c)]
						this.setData({
							list:c,
							data: a,
							time: res.data.data.AddTime,
							value: res.data.data.Millions
						})
					}
				})
			}
		})
		
	},

	bindTimeChange: function(e) {
		console.log(e)
		this.setData({
			time: e.detail.value
		})
	},
	journey(e) {
		this.setData({
			journey: e.detail.value
		})
	},
	gb(e) {
		let a = this.data.data
		let b = this.data.list
		console.log(b)
		a.forEach(function(item, index) {
			if (e.currentTarget.dataset.id == index) {
				if (a[index].state == undefined || a[index].state == 0) {
					a[index].state = 1
					b.push(a[index].names)
				} else {
					a[index].state = 0
					b.forEach(function(item, c) {
						if (b[c] == a[index].names) {
							b.splice(c, 1)
						}
					})
				}
			}
		})
		console.log(b)
		this.setData({
			data: a,
			list: b
		})
	},
	over() {
		console.log(this.data.time)
		console.log(this.data.journey)
		console.log(this.data.list.length)
		let a=''
		if(this.data.journey != undefined){
			a=this.data.journey
		}else{
			a=this.data.value
		}
		if (this.data.time != undefined  && this.data.list.length != 0) {
			let b = this.data.list
			b.join(',')
			wx.request({
				url: getApp().globalData.http + 'action=updateBaoYang&id=' + this.data.id + '&bytime=' +
					this.data.time + '&millions=' + a + '&byxm=' + b,
				success: (e) => {
					console.log(e)
				}
			})
		} else {
			wx.showToast({
				icon: 'none',
				title: '有未选项'
			})
		}

	},
	sc(){
		console.log(this.data.id)
		wx.request({
			url:getApp().globalData.http+'action=delBaoYang&id='+this.data.id,
			success:(e)=>{
				console.log(e)
				if(e.data.code!=1){
					wx.showToast({
						icon:'none',
						title:e.data.msg
					})
				}
			}
		})
	}
})
